package com.xinchuang.util;

import java.util.Arrays;
import java.util.List;

/**
 * TODO 经纬度相关工具类
 *
 * @author zkwczx
 * @version 1.0
 * @date 2022/10/23 15:52
 */
public class DeoUtil {

    /*
     * 大地坐标系资料WGS-84 长半径a=6378137 短半径b=6356752.3142 扁率f=1/298.2572236
     */
    /**
     * 长半径a=6378137
     */
    private static double a = 6378137;
    /**
     * 短半径b=6356752.3142
     */
    private static double b = 6356752.3142;
    /**
     * 扁率f=1/298.2572236
     */
    private static double f = 1 / 298.2572236;

    /**
     * 计算另一点经纬度
     *
     * @param lon  经度
     * @param lat  维度
     * @param brng 方位角
     * @param dist 距离（米）
     */
    public static List<Double> computerThatLonLat(double lon, double lat, double brng, double dist) {

        double alpha1 = rad(brng);
        double sinAlpha1 = Math.sin(alpha1);
        double cosAlpha1 = Math.cos(alpha1);

        double tanU1 = (1 - f) * Math.tan(rad(lat));
        double cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1));
        double sinU1 = tanU1 * cosU1;
        double sigma1 = Math.atan2(tanU1, cosAlpha1);
        double sinAlpha = cosU1 * sinAlpha1;
        double cosSqAlpha = 1 - sinAlpha * sinAlpha;
        double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
        double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
        double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));

        double cos2SigmaM = 0;
        double sinSigma = 0;
        double cosSigma = 0;
        double sigma = dist / (b * A), sigmaP = 2 * Math.PI;
        while (Math.abs(sigma - sigmaP) > 1e-12) {
            cos2SigmaM = Math.cos(2 * sigma1 + sigma);
            sinSigma = Math.sin(sigma);
            cosSigma = Math.cos(sigma);
            double deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)
                    - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
            sigmaP = sigma;
            sigma = dist / (b * A) + deltaSigma;
        }

        double tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
        double lat2 = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1,
                (1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp));
        double lambda = Math.atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
        double C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
        double L = lambda - (1 - C) * f * sinAlpha
                * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
        // final bearing
        double revAz = Math.atan2(sinAlpha, -tmp);
        return Arrays.asList(lon + deg(L), deg(lat2));
    }

    /**
     * 度换成弧度
     *
     * @param d 度
     * @return 弧度
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 弧度换成度
     *
     * @param x 弧度
     * @return 度
     */
    private static double deg(double x) {
        return x * 180 / Math.PI;
    }

    /**
     * 求两点连线与正北方向的角度（水平角度）
     *
     * @return
     */
    public double computeAzimuth(double lat1, double lon1, double lat2, double lon2) {
        double result = 0.0;
        int ilat1 = (int) (0.50 + lat1 * 360000.0);
        int ilat2 = (int) (0.50 + lat2 * 360000.0);
        int ilon1 = (int) (0.50 + lon1 * 360000.0);
        int ilon2 = (int) (0.50 + lon2 * 360000.0);
        lat1 = Math.toRadians(lat1);
        lon1 = Math.toRadians(lon1);
        lat2 = Math.toRadians(lat2);
        lon2 = Math.toRadians(lon2);
        if ((ilat1 == ilat2) && (ilon1 == ilon2)) {
            return result;
        } else if (ilon1 == ilon2) {
            if (ilat1 > ilat2) {
                result = 180.0;
            }
        } else {
            double c = Math.acos(Math.sin(lat2) * Math.sin(lat1) + Math.cos(lat2) * Math.cos(lat1) * Math.cos((lon2 - lon1)));
            double A = Math.asin(Math.cos(lat2) * Math.sin((lon2 - lon1)) / Math.sin(c));
            result = Math.toDegrees(A);
            if ((ilat2 > ilat1) && (ilon2 > ilon1)) {
            } else if ((ilat2 < ilat1) && (ilon2 < ilon1)) {
                result = 180.0 - result;
            } else if ((ilat2 < ilat1) && (ilon2 > ilon1)) {
                result = 180.0 - result;
            } else if ((ilat2 > ilat1) && (ilon2 < ilon1)) {
                result += 360.0;
            }
        }
        return result;
    }

    /**
     * 获取俯仰角度
     *
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     * @param h
     * @return
     */
    public static double getTilAngle(double lon1, double lat1, double lon2, double lat2, double h) {
        double l = DistanceUtil.getDistance(lat1, lon1, lat2, lon2);
        return Math.toDegrees(Math.atan(l / h));
    }

    /**
     * 获取云台和目标的直线距离（斜边长）
     *
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     * @param h
     * @return
     */
    public static double getDistance(double lon1, double lat1, double lon2, double lat2, double h) {
        double l = DistanceUtil.getDistance(lat1, lon1, lat2, lon2);
        return Math.sqrt(Math.pow(h, 2) + Math.pow(l, 2));
    }

    /**
     * 根据俯仰角计算水平距离 俯仰角小于45度时适用
     *
     * @param h     单位米
     * @param angle 单位度
     * @return
     */
    public static double HorizontalAngleCal(double h, double angle) {
        return h * Math.tan(angle);
    }

}
